package com.zt.bigdata.helloclient.controller;


import com.zt.bigdata.entity.User;
import com.zt.bigdata.helloclient.mongo.MongoUserService;
import com.zt.bigdata.helloclient.service.UserService;
import com.zt.bigdata.util.JsonHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: zhangtong
 * @Date: 2018/9/11 上午10:24
 */
@RestController
public class Controller {

    private static final Logger logger = LoggerFactory.getLogger(Controller.class);
    @Autowired
    UserService userService;
    @Autowired
    MongoUserService mongoUserService;
    @Value("${server.port}")
    private int port;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    private int[] code = new int[]{200, 500, 404, 503, 401};

    @PostMapping("/add")
    public Integer add(@RequestBody User user) {
        logger.info("新增用户");
        return userService.add(user);
    }

    @GetMapping("/list/{sex}")
    public void list(@PathVariable Integer sex,
                     @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                     @RequestParam("pageSize") Integer pageSize) {

//        return "";
    }

    @GetMapping("/cache/{sex}")
    public String cache(@PathVariable Integer sex,
                        @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                        @RequestParam("pageSize") Integer pageSize) {
        JsonHelper jsonHelper = new JsonHelper();
        String value;
        BoundValueOperations<String, String> x = redisTemplate.boundValueOps("list");
        value = x.get();
        if (value != null) {
            return value;
        }
        value = jsonHelper.toJsonString(userService.list(sex, pageIndex, pageSize));
        x.set(value);
        return value;
    }

    @GetMapping("/getUser")
    public void get(HttpServletResponse response) {
        response.setStatus(code[((int) Thread.currentThread().getId()) % 5]);
    }

    @PutMapping("/addUser")
    public void add(HttpServletResponse response) {
        response.setStatus(code[((int) Thread.currentThread().getId()) % 5]);
    }

    @PostMapping("/updateUser")
    public void update(HttpServletResponse response) {
        response.setStatus(code[((int) Thread.currentThread().getId()) % 5]);
    }

    @DeleteMapping("/deleteUser")
    public void delete(HttpServletResponse response) {
        response.setStatus(code[((int) Thread.currentThread().getId()) % 5]);
    }
}
